home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / nasanets.zip / CONVERT.C < prev    next >
C/C++ Source or Header  |  1990-06-07  |  6KB  |  180 lines

  1. /*=============================*/
  2. /*           NETS              */
  3. /*                             */
  4. /* a product of the AI Section */
  5. /* NASA, Johnson Space Center  */
  6. /*                             */
  7. /* principal author:           */
  8. /*       Paul Baffes           */
  9. /*                             */
  10. /* contributing authors:       */
  11. /*      Bryan Dulock           */
  12. /*      Chris Ortiz            */
  13. /*=============================*/
  14.  
  15.  
  16. /*
  17. ----------------------------------------------------------------------
  18.   Conversion Code for the Net Program (Prefix = C_)
  19. ----------------------------------------------------------------------
  20.   This file is a collection place for all those routines are used for
  21.   converting between types.  This code is divided into 2 major sections:
  22.  
  23.   (1) include files
  24.   (2) subroutines
  25.  
  26.   Each section is further explained below.
  27. ----------------------------------------------------------------------
  28. */
  29.  
  30.  
  31. /*
  32. ----------------------------------------------------------------------
  33.   INCLUDE FILES
  34. ----------------------------------------------------------------------
  35. */
  36. #include  "common.h"
  37.  
  38.  
  39. /*
  40. ======================================================================
  41.   ROUTINES IN CONVERT.C                                                   
  42. ======================================================================
  43.   The routines in this file are grouped below by function.  Each routine
  44.   is prefixed by the string "C_" indicating that it is defined in the 
  45.   "convert.c" file.  The types returned by the routines are also shown 
  46.   here so that cross checking is more easily done between these functions
  47.   and the other files which intern them.
  48.  
  49.  
  50.   Type Returned                 Routine                                 
  51.   -------------                 -------                                 
  52.                                                                       
  53.   CONVERSION ROUTINES                                                   
  54.     Sint                        C_float_to_Sint
  55.     Sint                        C_double_to_Sint
  56.     Sint                        C_int_to_Sint
  57.     float                       C_Sint_to_float
  58.     double                      C_Sint_to_double
  59.     int                         C_Sint_to_int
  60. ======================================================================
  61. */
  62.  
  63.  
  64. Sint  C_float_to_Sint(num)
  65. float  num;
  66. /*
  67. ----------------------------------------------------------------------
  68.  This routine converts a floating point number into our internal rep- 
  69.   resentation for floating point.  The whole reason behind all this   
  70.   trouble is the goal of being able to do floating point arithmetic   
  71.   with integers (because integer operations are faster).  Since most  
  72.   of the time spent by the teaching phase will be spent doing this    
  73.   type of arithmetic (probably 90% of the time!), using integers is   
  74.   extremely important.                                                
  75.  To convert is straightforward: first, consider the fact that you have
  76.   a decimal number, ie some number / 10.  To convert to the equivalent
  77.   Sint representation, just use dimensional analysis a-la chemistry 1 
  78.   where you have  x_decimal/10 = x_Sint/1024.  Thus x_Sint is just    
  79.   x_decimal/10 * 1024. Since floats are already in decimal form (ie,  
  80.   3.2 in decimal is 3.2 not 3.2/10) all we need to do is return       
  81.   num * 1024 (cast into the correct type, of course).                 
  82. ----------------------------------------------------------------------
  83. */
  84. BEGIN
  85. #if  USE_SCALED_INTS
  86.    return( (Sint) (num * (float)SINT_SCALE) );
  87. #else
  88.    return((Sint)num);
  89. #endif
  90.  
  91. END /* C_float_to_Sint */
  92.  
  93.  
  94. Sint  C_double_to_Sint(num)
  95. double  num;
  96. /*
  97. ----------------------------------------------------------------------
  98.  This routine converts a double into a Sint                           
  99. ----------------------------------------------------------------------
  100. */
  101. BEGIN
  102. #if  USE_SCALED_INTS
  103.    return( (Sint) (num * (double)SINT_SCALE) );
  104. #else
  105.    return((Sint)num);
  106. #endif
  107.  
  108. END /* C_double_to_Sint */
  109.  
  110.  
  111. Sint  C_int_to_Sint(num)
  112. int  num;
  113. /*
  114. ----------------------------------------------------------------------
  115.  This routine converts an integer into a Sint.                        
  116. ----------------------------------------------------------------------
  117. */
  118. BEGIN
  119. #if  USE_SCALED_INTS
  120.    return( (Sint) (num * SINT_SCALE) );
  121. #else
  122.    return( (Sint)num );
  123. #endif
  124.  
  125. END /* C_int_to_Sint */
  126.  
  127.  
  128. float  C_Sint_to_float(num)
  129. Sint  num;
  130. /*
  131. ----------------------------------------------------------------------
  132.  converts a Sint (special integer) to a  floating point number. Note 
  133.  that this cannot be done with right-shifting since that would throw
  134.  away all the decimal information contained in the Sint.
  135. ----------------------------------------------------------------------
  136. */
  137. BEGIN
  138. #if  USE_SCALED_INTS
  139.    return( (float) (num / (float)SINT_SCALE) );
  140. #else
  141.    return( (float)num );
  142. #endif
  143.  
  144. END /* C_Sint_to_float */
  145.  
  146.  
  147. double  C_Sint_to_double(num)
  148. Sint  num;
  149. /*
  150. ----------------------------------------------------------------------
  151.  converts a Sint (special integer) to a double floating point number  
  152. ----------------------------------------------------------------------
  153. */
  154. BEGIN   
  155. #if  USE_SCALED_INTS
  156.    return( (double) (num / (double)SINT_SCALE) );
  157. #else
  158.    return( (double)num );
  159. #endif
  160.  
  161. END /* C_Sint_to_double */
  162.  
  163.  
  164. int  C_Sint_to_int(num)
  165. Sint  num;
  166. /*
  167. ----------------------------------------------------------------------
  168.  converts a Sint (special integer) to an integer for printing         
  169. ----------------------------------------------------------------------
  170. */
  171. BEGIN
  172. #if  USE_SCALED_INTS
  173.    return( (int) (num / SINT_SCALE) );
  174. #else
  175.    return( (int)num );
  176. #endif
  177.  
  178. END /* C_Sint_to_int */
  179.  
  180.